#!/usr/bin/env bash

help(){
    cat<<__EOF__
Usage: m finder [OPTIONS]

Description: Manage Finder settings on macOS

Options:
 --help                                      Show this help message
 --showhiddenfiles <status|enable|disable>   Show or hide hidden files in Finder
 --showextensions  <status|enable|disable>   Show or hide file extensions in Finder
 --showdesktop <status|enable|disable>       Enable or disable the desktop in Finder
 --showpath    <status|enable|disable>       Show or hide the current folder path in Finder's title bar
__EOF__
}

hidden_files(){
    case $1 in
        enable)
            defaults write com.apple.finder AppleShowAllFiles -bool true && \
                echo "Show hidden files: enabled"
            ;;
        disable)
            defaults write com.apple.finder AppleShowAllFiles -bool false && \
                echo "Show hidden files: disabled"
            ;;
        status)
            HIDDEN_FILE_STATUS=$(defaults read com.apple.finder AppleShowAllFiles 2>/dev/null)

            case $HIDDEN_FILE_STATUS in
                0|[nN][oO]|[fF][aA][lL][sS][eE])
                    HIDDEN_FILE_STATUS="disabled"
                    ;;
                1|[yY][eE][sS]|[tT][rU][eE])
                    HIDDEN_FILE_STATUS="enabled"
                    ;;
                *)
                    echo "We can't read AppleShowAllFiles property" && exit 1
                    ;;
            esac
            echo "Show hidden files: $HIDDEN_FILE_STATUS" && exit 0
            ;;
        *)
            help && exit 1
    esac
    killall Finder
}

file_extensions(){
    case $1 in
        enable)
            defaults write NSGlobalDomain AppleShowAllExtensions -bool true && \
                echo "Show file extensions: enabled"
            ;;
        disable)
            defaults write NSGlobalDomain AppleShowAllExtensions -bool false && \
                echo "Show file extensions: disabled"
            ;;
        status)
            EXTENSION_STATUS=$(defaults read NSGlobalDomain AppleShowAllExtensions 2>/dev/null)
            case $EXTENSION_STATUS in
                0|[nN][oO]|[fF][aA][lL][sS][eE])
                    EXTENSION_STATUS="disabled"
                    ;;
                1|[yY][eE][sS]|[tT][rU][eE])
                    EXTENSION_STATUS="enabled"
                    ;;
                *)
                    echo "We can't read AppleShowAllExtension property" && exit 1
                    ;;
            esac
            echo "Show file extensions: $EXTENSION_STATUS"
            exit 0
            ;;
        *)
            help && exit 1
            ;;
    esac
    killall Finder
}

desktop(){
    case $1 in
        enable)
            defaults write com.apple.finder CreateDesktop -bool true && \
                echo "Desktop: enabled"
            ;;
        disable)
            defaults write com.apple.finder CreateDesktop -bool false && \
                echo "Desktop: disabled"
            ;;
        status)
            DESKTOP_STATUS=$(defaults read com.apple.finder CreateDesktop 2>/dev/null)
            case $DESKTOP_STATUS in
                0|[nN][oO]|[fF][aA][lL][sS][eE])
                    DESKTOP_STATUS="disabled"
                    ;;
                1|[yY][eE][sS]|[tT][rU][eE])
                    DESKTOP_STATUS="enabled"
                    ;;
                *)
                    echo "We can't read CreateDesktop property" && exit 1
                    ;;
            esac
            echo "Desktop: $DESKTOP_STATUS"
            ;;
        *)
            help && exit 1
            ;;
    esac
    killall Finder
}

path(){
    case $1 in
        enable)
            defaults write com.apple.finder _FXShowPosixPathInTitle -bool true && \
            echo "Show folder path: enabled"
            ;;
        disable)
            defaults write com.apple.finder _FXShowPosixPathInTitle -bool false && \
            echo "Show folder path: disabled"
            ;;
        status)
            EXTENSION_STATUS=$(defaults read com.apple.finder _FXShowPosixPathInTitle 2>/dev/null)
            case $EXTENSION_STATUS in
                0|[nN][oO]|[fF][aA][lL][sS][eE])
                    EXTENSION_STATUS="disabled"
                    ;;
                1|[yY][eE][sS]|[tT][rU][eE])
                    EXTENSION_STATUS="enabled"
                    ;;
                *)
                    echo "We can't read _FXShowPosixPathInTitle property" && exit 1
                    ;;
            esac
            echo "Show folder path: $EXTENSION_STATUS"
            exit 0
            ;;
        *)
            help && exit 1
            ;;
    esac
    killall Finder
}


# Main script execution
option="${1}"
shift

case "${option}" in
    --help)
        help
        ;;
    --showhiddenfiles)
        hidden_files "$@"
        ;;
    --showextensions)
        file_extensions "$@"
        ;;
    --showdesktop)
        desktop "$@"
        ;;
    --showpath)
        path "$@"
        ;;
    *)
        help && exit 1
        ;;
esac
